package team.hyznrj.studentsys.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.annotations.Expose;
import com.mongodb.BasicDBObject;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import team.hyznrj.studentsys.exception.DataNullException;

/**
 * 成绩、操行分、学生信息存放类
 * 
 * @author 郑泽锋
 * @date 2018年5月2日
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(doNotUseGetters = true)
public class SGQEntity extends StudentBasicMessage {
	public transient final static String[] ORDER_NAME = { "专业文化素质名次", "思想道德素质名次", "社会实践能力名次", "总评名次" };
	@Expose
	public transient static final String pratice[] = { "科技创新", "技术技能", "组织管理", "特殊加分", "总分" };
	@Expose
	public transient static final String pratice_database[] = { "科技创新分", "技术技能分", "组织管理分", "特殊加分" };
	/**
	 * 学生总信息(虚拟映射)
	 */
	private transient BasicDBObject stuMessage;
	/**
	 * 总映射
	 */
	// private Map<String,String> otherMessage;
	/**
	 * 操行分
	 */
	private QualityEntity qualityEntity;
	/**
	 * 参数
	 */
	private transient ExcelStaticParam param;
	/**
	 * 成绩
	 */
	private CourseEntity courseEntity;
	/**
	 * 奖学金等级 1:一等奖 2:二等奖 3:三等奖
	 */
	private Integer reward;
	/**
	 * 总计分
	 */
	private Double allMainCount = -1.0;

	public String getReward() {
		if (reward == null) {
			return null;
		}
		switch (reward) {
		case 1: {
			return "一";
		}
		case 2: {
			return "二";
		}
		case 3: {
			return "三";
		}
		default: {
			return null;
		}
		}
	}

	/**
	 * 获取学年总评分
	 * 
	 * @return
	 */
	public Double getAllMainCount() {
		if (allMainCount == -1.0) {
			double sQ = 0.0;
			double mor = 0.0;
			Double peQuality = getPeQuality();
			Double get_AvgCultrualGrade = get_AvgCultrualGrade();
			List<Double> socalQuality = getSocalQuality();
			List<Double> morality = getMorality();
			//社会实践分
			if (socalQuality.size() == 5) {
				sQ = socalQuality.get(4);
			}
			//
			if (morality.size() == 4) {
				mor = morality.get(3);
			}
			if (peQuality == null) {
				peQuality = 0.0;
			}
			if (get_AvgCultrualGrade == null) {
				get_AvgCultrualGrade = 0.0;
			}
			//平均学分+体育分+思想道德素质分+社会实践能力分
			allMainCount = get_AvgCultrualGrade+sQ + peQuality  + mor;
		}
		return allMainCount;
	}

	/**
	 * 社会实践能力分数组
	 */
	private List<Double> SOCALQUALITY_LIST;
	/**
	 * 思想道德分数组
	 */
	private List<Double> MORALITY_LIST;
	/**
	 * order
	 */
	private Map<String, Integer> myOrder;
	/**
	 * 是否忽略
	 */
	private transient boolean ignore;

	/**
	 * 获取社会实践能力分
	 * 
	 * @return
	 */
	public List<Double> getSocalQuality() {
		if (SOCALQUALITY_LIST == null) {
			SOCALQUALITY_LIST = new ArrayList<>();
			Map<String, QualityMessage> qualityMessages = null;
			if (qualityEntity != null) {
				qualityMessages = qualityEntity.getQualityMessages();
			}
			double mainCount = 0;
			if (qualityMessages != null) {
				for (int i = 0; i < pratice.length - 1; i++) {
					double count = 0;
					QualityMessage qualityMessage = qualityMessages.get(pratice_database[i]);
					if (qualityMessage != null) {
						count = qualityMessage.getCount();
						if(param.isDIV_TWO()&&! ((i==2)&&!param.isDIV_ORIGINATION())){
							count /= 2;
						}
						if (i == 0 && count > param.getPARTICE_MAX_INNOVATION()) {
							count = param.getPARTICE_MAX_INNOVATION();
						} else if (i == 1 && count > param.getPARTICE_MAX_SKILL()) {
							count = param.getPARTICE_MAX_SKILL();
						} else if (i == 2 && count > param.getPARTICE_MAX_ORGINATION()) {
							count = param.getPARTICE_MAX_ORGINATION();
						} else if (i == 3 && count > param.getPARTICE_MAX_SPECIAL()) {
							count = param.getPARTICE_MAX_SPECIAL();
						}
						mainCount += count;
					}
					SOCALQUALITY_LIST.add(count);
				}
				SOCALQUALITY_LIST.add(mainCount);
			} else {
				for (int i = 0; i < pratice.length; i++) {
					SOCALQUALITY_LIST.add(0.0);
				}
			}
		}
		return SOCALQUALITY_LIST;
	}

	/**
	 * 获取身体素质分 null表示不存在
	 * 
	 * @return
	 */
	public Double getPeQuality() {
		Double peMain = null;
		if (courseEntity != null) {
			if (courseEntity.getCountPE() > 0) {
				double peGrade = courseEntity.getPeGrade();//8
				int countPE = courseEntity.getCountPE();//2
				peMain = (peGrade > 0 ? ((peGrade/countPE) * 10 + 50) : 0)* param.getEXCEL_PE_RATE();
			}
		}//4.5 2 4.5   
		return peMain;
	}

	/**
	 * 获得思想道德素质分 按基本分、奖励分、扣除分、总分
	 * 
	 * @return
	 */
	private static final String[] POLITY_MOLITY_NAME = { "思想政治分", "道德品质分" };

	public List<Double> getMorality() {
		if (MORALITY_LIST == null) {
			MORALITY_LIST = new ArrayList<>();
			MORALITY_LIST.add(param.getEXCEL_BASE_MORALITY());
			double positive_count = 0;
			double negetive_count = 0;
			double basic_count = param.getEXCEL_BASE_MORALITY();
			if (qualityEntity != null) {
				Map<String, QualityMessage> qualityMessages = qualityEntity.getQualityMessages();
				if (qualityMessages != null) {
					for (int i = 0; i < POLITY_MOLITY_NAME.length; i++) {
						QualityMessage qualityMessage = qualityMessages.get(POLITY_MOLITY_NAME[i]);
						if (qualityMessage != null) {
							double tmpPositive = Math.abs(qualityMessage.getPositive_count());
							double tmpNegative = Math.abs(qualityMessage.getNegetive_count());
							if (param.isDIV_TWO()) {
								tmpPositive /= 2;
								tmpNegative /= 2;
							}
							double tmp=0;
							//获取最高分
							if(i==0){
								tmp=param.getMORALITY_MAX_ADD1();
							}else{
								tmp=param.getMORALITY_MAX_ADD2();
							}
							if (tmpPositive >= tmp) {
								positive_count = tmp;
							} else {
								positive_count += tmpPositive;
							}
							negetive_count += tmpNegative;
							if (negetive_count >= param.getMORALITY_MAX_DEC()) {
								negetive_count = param.getMORALITY_MAX_DEC();
							}
						}
					}
				}
			}
			basic_count=basic_count+positive_count-negetive_count;
			
			MORALITY_LIST.add(positive_count);
			MORALITY_LIST.add(negetive_count);
			MORALITY_LIST.add(basic_count);
		}
		return MORALITY_LIST;
	}

	/**
	 * 获得专业文化素质分
	 * 
	 * @param param
	 * @return double
	 */
	public Double get_AvgCultrualGrade() {
		if (courseEntity == null || courseEntity.isNullEntity()) {
			/** 没有导入成绩，则记NULL */
			ignore = true;
			return null;
		}
		double avgGrade = courseEntity.getAvgGrade();
		if (avgGrade > 0) {
			double v = avgGrade * 10 + 50;
			if (param != null) {
				if (courseEntity.getCountPE() > 0) {
					v *= param.getEXCEL_AVG_CULTURE_GRADE();
				} else {
					v *= param.getEXCEL_AVG_CULTURE_GRADE_WITHOUT_PE();
				}
			}
			return v;
		}
		return null;
	}

	/**
	 * 获取学生挂科数
	 */
	public int getGGNum() {
		return courseEntity == null ? -1 : courseEntity.getGgNum();
	}

	/**
	 * 获取学生某科目分数
	 * 
	 * @param title
	 * @return
	 */
	public Double getCourseValue(final String title) {
		Map<String, Double> allCourse = courseEntity.getAllCourse();
		return allCourse.get(title);
	}

	/**
	 * 根据虚拟映射设置实体映射
	 * 
	 * @param stuMessage
	 * @param idMap
	 */
	public void setStuMessage(BasicDBObject stuMessage, Map<String, String> map) {
		if (stuMessage == null) {
			throw new DataNullException("StuMessage数据为空");
		}
		this.stuMessage = stuMessage;
		/**
		 * 获取基本信息
		 */
		this.sno = stuMessage.getString(map.get("学号"));
		this.sname = stuMessage.getString(map.get("姓名"));
		this.academy = stuMessage.getString(map.get("学院"));
		this.clas = stuMessage.getString(map.get("班级"));
		this.major = stuMessage.getString(map.get("专业"));
	}
}
